﻿using System;
using System.Collections.Generic;
using System.Text;

namespace ST.Common.Util.Office
{
	public class ExcelColumn
	{
		/// <summary>
		/// 数据库列名/实体属性名
		/// </summary>
		public string PropertyName { get; set; }

		/// <summary>
		/// 实体属性反射索引（以0开始）
		/// </summary>
		public int PropertyIndex { get; set; }

		/// <summary>
		/// 数据库值/实体属性值
		/// </summary>
		public object PropertyValue { get; set; }

		/// <summary>
		/// Excel列名称
		/// </summary>
		public string ColumnName { get; set; }

		/// <summary>
		/// 列类型（d：日期，n：数字，m：货币）
		/// </summary>
		public ExcelColumnType ColumnType { get; set; }

		/// <summary>
		/// 列格式化字符串
		/// </summary>
		public string ColumnFormat { get; set; }

		/// <summary>
		/// Excel列宽度
		/// </summary>
		public int ColumnWidth { get; set; }

		/// <summary>
		/// Excel中列索引
		/// </summary>
		public int ColumnIndex { get; set; }

		/// <summary>
		/// 是否忽略导入、导出
		/// </summary>
		public bool Ignore { get; set; }

		/// <summary>
		/// 单元列合并
		/// </summary>
		public int ColumnSpan { get; set; }

		/// <summary>
		/// 单元格水平对齐方式
		/// </summary>
		public int ColumnHAlign { get; set; }

		/// <summary>
		/// 为“是”时的值
		/// </summary>
		public string BooleanYes { get; set; }

		/// <summary>
		/// 为“否”时的值
		/// </summary>
		public string BooleanNo { get; set; }

		/// <summary>
		/// Converted property value to Int32
		/// </summary>
		public int IntValue
		{
			get
			{
				if (PropertyValue == null || !int.TryParse(PropertyValue.ToString(), out var rez))
					return default(int);
				return rez;
			}
		}

		/// <summary>
		/// Converted property value to boolean
		/// </summary>
		public bool BooleanValue
		{
			get
			{
				string val = Convert.ToString(PropertyValue);
				if (val == BooleanYes || val == "1" || val == "true" || val == "True")
				{
					return true;
				}
				else if (val == BooleanNo)
				{
					return false;
				}
				else
				{
					return default(bool);
				}
			}
		}

		/// <summary>
		/// Converted property value to string
		/// </summary>
		public string StringValue => PropertyValue == null ? string.Empty : Convert.ToString(PropertyValue);

		/// <summary>
		/// Converted property value to decimal
		/// </summary>
		public decimal DecimalValue
		{
			get
			{
				if (PropertyValue == null || !decimal.TryParse(PropertyValue.ToString(), out var rez))
					return default(decimal);
				return rez;
			}
		}

		/// <summary>
		/// Converted property value to decimal?
		/// </summary>
		public decimal? DecimalValueNullable
		{
			get
			{
				if (PropertyValue == null || !decimal.TryParse(PropertyValue.ToString(), out var rez))
					return null;
				return rez;
			}
		}

		/// <summary>
		/// Converted property value to double
		/// </summary>
		public double DoubleValue
		{
			get
			{
				if (PropertyValue == null || !double.TryParse(PropertyValue.ToString(), out var rez))
					return default(double);
				return rez;
			}
		}

		/// <summary>
		/// 
		/// </summary>
		public DateTime DateTimeValue
		{
			get
			{
				if (PropertyValue == null || !DateTime.TryParse(PropertyValue.ToString(), out var rez))
					return DateTime.MinValue;
				return rez;
			}
		}

		/// <summary>
		/// 
		/// </summary>
		public DateTime? DateTimeValueNullable
		{
			get
			{
				if (PropertyValue == null || !DateTime.TryParse(PropertyValue.ToString(), out var rez))
					return null;
				return rez;
			}
		}

		///// <summary>
		///// Converted property value to DateTime?
		///// </summary>
		//public DateTime? DateTimeNullable => PropertyValue == null ? null : DateTime.FromOADate(DoubleValue) as DateTime?;

		public ExcelColumn()
		{
			this.ColumnIndex = -1;
		}

		public ExcelColumn(string propertyName, string columnName)
		{
			this.PropertyName = propertyName;
			this.ColumnName = columnName;
			this.ColumnType = ExcelColumnType.String;
			this.ColumnIndex = -1;
		}

		public ExcelColumn(string propertyName, string columnName, int columnIndex)
		{
			this.PropertyName = propertyName;
			this.ColumnName = columnName;
			this.ColumnType = ExcelColumnType.String;
			this.ColumnIndex = columnIndex;
		}

		public ExcelColumn(string propertyName, string columnName, string propertyValue)
		{
			this.PropertyName = propertyName;
			this.ColumnName = columnName;
			this.ColumnType = ExcelColumnType.String;
			this.PropertyValue = propertyValue;
		}

		public ExcelColumn(string propertyName, string columnName, ExcelColumnType columnType)
		{
			this.PropertyName = propertyName;
			this.ColumnName = columnName;
			this.ColumnType = columnType;
			this.ColumnIndex = -1;
		}

		public ExcelColumn(string propertyName, string columnName,  string booleanYes, string booleanNo)
		{
			this.PropertyName = propertyName;
			this.ColumnName = columnName;
			this.ColumnType = ExcelColumnType.Bool;
			this.ColumnIndex = -1;
			this.BooleanYes = booleanYes;
			this.BooleanNo = booleanNo;
		}

		public ExcelColumn(string propertyName, string columnName, ExcelColumnType columnType, int columnIndex)
		{
			this.PropertyName = propertyName;
			this.ColumnName = columnName;
			this.ColumnType = columnType;
			this.ColumnIndex = columnIndex;
		}

		public ExcelColumn(string propertyName, string columnName, ExcelColumnType columnType, string columnFormat, int columnWidth, int columnIndex)
		{
			this.PropertyName = propertyName;
			this.ColumnName = columnName;
			this.ColumnType = columnType;
			this.ColumnFormat = columnFormat;
			this.ColumnWidth = columnWidth;
			this.ColumnIndex = columnIndex;
		}
	}
}
